/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clip;

import L.L;
import clip.gui.ClipTrayIcon;
import clip.gui.MainWindow;

import clip.model.Buddy;
import clip.model.BuddyList;
import clip.model.Options;
import clip.net.RMIServer;
import java.awt.Toolkit;
import java.awt.datatransfer.Transferable;
import java.io.PrintStream;
import java.util.Observable;
import java.util.Observer;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.jvnet.substance.skin.SubstanceNebulaLookAndFeel;


public class clipApp implements Observer {

    public MainWindow clipWin;
    public BuddyList buddyList = new BuddyList();
    public Options options = new Options(this);
    public RMIServer rmiServer;
    public ClipTrayIcon trayIcon;

    public clipApp() {
        L l = new L();
        l.setLOGLEVEL(L.LOGLEVEL_SUPERDEBUG);
        //system.out umleiten
        catchSystemOut();

        // ZUM TESTEN
        //initTest();
        final clipApp app = this;
        //Window
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                try {
                    UIManager.setLookAndFeel(new SubstanceNebulaLookAndFeel());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                clipWin = new MainWindow(app);
                clipWin.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                clipWin.setSize(250, 500);
                clipWin.setVisible(true);
            }
        });
        //TrayIcon
        trayIcon = new ClipTrayIcon(this);

        //Server starten
        rmiServer = new RMIServer(this);
    }

    public static void main(String[] args) {
        new clipApp();
    }

    void registerObservers() {
        buddyList.addObserver(this);
        buddyList.addObserver(this.clipWin);
    }

    private void catchSystemOut() {
        
    }

    private void initTest() {
        //FÜRS TESTEN
        buddyList.add(new Buddy("luk", "192.168.2.65", 1099));
        buddyList.add(new Buddy("tob", "192.168.2.23", 1099));

    }

    public void update(Observable o, Object arg) {
        L.o("update von observable", o.getClass().getSimpleName(), arg.toString());
    }

    public void setSystemClipboard(Transferable c) {
        try {
            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(c, null);
            L.d("set clipboard to" + c.toString());
        } catch (Exception e) {
            L.e("ERROR BEIM CLIPBOARD SETZEN!!!");
        }
    }

    public class ReconnectTimer extends TimerTask {

        private clipApp app;
        private Timer timer = new Timer();

        public ReconnectTimer(clipApp app) {
            this.app = app;
            timer.schedule(this, 4000, 60 * 1000);
        }

        @Override
        public void run() {
            app.buddyList.tryToConnectToAllUnconnected();
        }
    }
}
